#include"SList.h"

SL* BuySListNode(SLTDateType x) {
	SL* cur = (SL*)malloc(sizeof(SL));
	cur->data = x;
	cur->next = NULL;
	return cur;
}

void SListPrint(SL* plist) {
	SL* cur = plist;
	while (cur) {
		printf("%d  ", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void SListPushBack(SL** pplist, SLTDateType x) {
	SL* news = BuySListNode(x);
	if (*pplist == NULL) {
		*pplist = news;
	}
	SL* tail = *pplist;
	while (tail->next != NULL) {
		tail = tail->next;
	}
	tail->next = news;
}


void SListPushFront(SL** pplist, SLTDateType x) {
	SL* news= BuySListNode(x);
	news->next = *pplist;
	*pplist = news;
}

void SListPopBack(SL** pplist) {
	assert(*pplist);
	SL* tail = *pplist;
	if (( * pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	while (tail->next->next != NULL)tail = tail->next;
	free(tail->next);
	tail->next = NULL;
}

void SListPopFront(SL** pplist) {
	assert(*pplist);
	if (( * pplist)->next == NULL) {
		free(*pplist);
		*pplist == NULL;
	}
	SL* first = *pplist;
	*pplist = first->next;
	free(first);
	first = NULL;
}

SL* SListFind(SL* plist, SLTDateType x) {
	SL* cur = plist;
	while (cur) {
		if (cur->data == x)return cur;
		cur = cur->next;
	}
	return NULL;
}

void SListInsertAfter(SL* pos, SLTDateType x) {
	assert(pos);
	SL* cur = BuySListNode(x);
	cur->next = pos->next;
	pos->next = cur;
}

void SListEraseAfter(SL* pos) {
	assert(pos&&pos->next);
	SL* cur = pos->next;
	pos->next = pos->next->next;

}